package Servlet;

import com.cyy.dao.cartDao;
import com.cyy.dao.collectDao;
import com.cyy.dao.historyDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


@WebServlet({"/deleteCart","/deleteHistory","/deleteCollect"})
public class deleteServelt extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String productName=request.getParameter("productName");
        String userName=request.getSession().getAttribute("name").toString();

        String uri=request.getRequestURI();
        if(uri.equals("/deleteCart")) {
            cartDao cartDao = new cartDao();
            try {
                cartDao.deleteByUserAndProduct(userName, productName);
            } catch (SQLException | ClassNotFoundException throwables) {
                throwables.printStackTrace();
            }
            request.getRequestDispatcher("/showCart").forward(request, response);
        }
        else if(uri.equals("/deleteCollect")) {
            collectDao collectDao = new collectDao();
            try {
                collectDao.deleteByUserAndProduct(userName, productName);
            } catch (SQLException | ClassNotFoundException throwables) {
                throwables.printStackTrace();
            }
            request.getRequestDispatcher("/showCollect").forward(request, response);
        }
        else
        {
            String time=request.getParameter("time").replace("'","");
            historyDao historyDao = new historyDao();
            try {
                historyDao.deleteByUserAndProduct(userName,productName,time);
            } catch (SQLException | ClassNotFoundException throwables) {
                throwables.printStackTrace();
            }
            request.getRequestDispatcher("/showHistory").forward(request,response);
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
